<template>
  <div class="app-container">
    <!-- 联合查询 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.propName"
        placeholder="申请人"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.tel"
        placeholder="联系电话"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.decoration"
        placeholder="描述"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <!-- 枚举类型处理 -->
      <el-select
        v-model="listQuery.solve"
        placeholder="状态"
        clearable
        style="width: 90px"
        class="filter-item"
      >
        <el-option
          v-for="item in sloveOptions"
          :key="item.key"
          :label="item.label"
          :value="item.key"
        />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        查询
      </el-button>
    </div>
    <!-- 列表数据 -->
    <el-table
      ref="filterTable"
      :data="allList.slice((page-1)*limit,page*limit)"
      style="width: 100%"
    >
      <el-table-column
        prop="did"
        label="序号"
        sortable
        width="90"
      />
      <el-table-column
        prop="description"
        label="描述"
        width="180"
      />
      <el-table-column
        prop="urls"
        label="照片"
        width="250"
      >
        <template slot-scope="{row}">
          <div class="demo-image__lazy">
            <el-image v-for="item in row.urls" :key="item.key" style="width:50px;height: 50px;display: inline block;" :src="item.url" :preview-src-list="[item.url]" />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="propName"
        label="申请人"
      />
      <el-table-column
        prop="tel"
        label="联系电话"
      />
      <el-table-column
        prop="slove"
        label="状态"
      >
        <template slot-scope="{row}">
          <el-tag
            :type="row.solve == '0' ? 'danger' : 'success'"
            disable-transitions
          >
            {{ row.solve == 0 ? '未审核' : '已审核' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200"
      >
        <template slot-scope="{row}">
          <el-button
            size="small"
            type="success"
            @click="handlerUpdate(row.did)"
          ><span>处理</span></el-button>
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(row.did)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页处理 -->
    <el-pagination
      :current-page="page"
      :page-sizes="[5, 10, 20, 40]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { deleteDecoration, decorationList, updateDecoration } from '@/api/decoration'
import waves from '@/directive/waves'
export default {
  directives: { waves },
  inject: ['reload'], // 刷新列表渲染
  data() {
    return {
      allList: null,
      total: null,
      page: 1,
      limit: 5,
      listLoading: true,
      listQuery: {
        propName: '',
        description: '',
        tel: '',
        solve: ''
      },
      sloveOptions: [{ label: '未审核', key: '0' }, { label: '已审核', key: '1' }]
    }
  },
  mounted() { // mounted钩子函数（生命周期函数）相当window.onload函数，随页面一加载执行
    this.getAllList() // 单一网络请求在student.js经过封装统一处理，这里需要导入这个文件
  },
  created() {
    this.getAllList()
  },
  methods: {
    handleSizeChange: function(size) {
      this.limit = size // 每页下拉显示数据
    },
    handleCurrentChange: function(currentPage) {
      this.page = currentPage // 点击第几页
    },
    // 获取数据列表
    getAllList() {
      decorationList(this.listQuery).then(res => {
        this.allList = res.data
        this.total = res.data.length
      })
    },
    // 查询数据重新渲染列表
    handleFilter() {
      this.page = 1
      this.getAllList()
    },
    // 状态显示处理
    filterSolve(value, row) {
      return row.solve === value
    },
    // 删除装修申请记录
    handleDelete(did) {
      this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        deleteDecoration(did).then((res) => {
          if (res.code === 101) {
            this.$notify({
              title: 'Success',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.reload()
          }
        })
      }).catch(() => {
        this.$notify({
          title: 'Info',
          message: '取消删除',
          type: 'info'
        })
      })
    },
    // 更改装修审核状态
    handlerUpdate(repairID) {
      this.$confirm('更新审核状态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        updateDecoration(repairID).then((res) => {
          if (res.code === 101) {
            this.$notify({
              title: 'Success',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.reload()
          }
        })
      }).catch(() => {
        this.$notify({
          title: 'Info',
          message: '取消处理',
          type: 'info'
        })
      })
    }
  }
}
</script>

<style>

</style>
